package com.shuqi.activity.viewport;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;

import com.shuqi.controller.R;

/**
 * 底部弹出的对话框
 * 
 * @author yanghuawei
 */
public class BottomDialogNight extends Dialog implements android.view.View.OnClickListener {
    // private final String loagTag = "BottomDialog";
    private Context context;
    private Animation animationIn;
    private Animation animationOut;

    /** 弹入 动画的时长 */
    private final int animDuration_In = 100;
    /** 弹出 动画的时长 */
    private final int animDuration_Out = 100;

    /** 延时消息：隐藏对话框 */
    private final int MSG_DISSMIS = 0;

    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case MSG_DISSMIS:
                    BottomDialogNight.super.dismiss();
                    break;
            }
        };
    };

    /**
     * 初始化对话框：
     * 
     * @param context
     * @param contentView 对话框内要显示的view
     */
    public BottomDialogNight(Context context, View contentView) {
        super(context, R.style.MyDialogTransparentNight);
        this.context = context;
        View view = View.inflate(context, R.layout.view_dialog_bottom, null);
        setContentView(view);

        LinearLayout container = (LinearLayout) view.findViewById(R.id.dialog_bottom_container);
        LinearLayout.LayoutParams llparams =
                new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
        llparams.gravity = Gravity.CENTER;
        container.addView(contentView, llparams);

        Window window = this.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.windowAnimations = 0;
        // window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
        // WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        // window.addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        window.setAttributes(params);
        window.setGravity(Gravity.BOTTOM);
        window.setLayout(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        setCanceledOnTouchOutside(true);

        findViewById(R.id.dialog_bottom_gone).setOnClickListener(this);
    }

    @Override
    public void show() {
        if (animationIn == null) {
            animationIn = AnimationUtils.loadAnimation(context, R.anim.menu_push_top_in);
            animationIn.setDuration(animDuration_In);
        }
        findViewById(R.id.dialog_bottom_container).startAnimation(animationIn);
        super.show();
    }

    @Override
    public void dismiss() {
        if (animationOut == null) {
            animationOut = AnimationUtils.loadAnimation(context, R.anim.menu_push_top_out);
            animationOut.setDuration(animDuration_Out);
        }
        findViewById(R.id.dialog_bottom_container).startAnimation(animationOut);
        handler.sendEmptyMessageDelayed(MSG_DISSMIS, animDuration_Out);
    }

    private boolean isDismissOnClickOutSide = true;

    /***
     * 点击按钮外面是否隐藏对话框（默认隐藏）
     * 
     * @param isDissmiss
     */
    public void setDissmisOnClickOutSide(boolean isDissmiss) {
        isDismissOnClickOutSide = isDissmiss;
    }

    public void setGoneViewColor() {
        findViewById(R.id.dialog_bottom_gone).setBackgroundColor(0x00000000);
    }

    private boolean isInterceptMenuKey = false;

    public void setIsInterceptMenuKey(boolean isIntercept) {
        isInterceptMenuKey = isIntercept;
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (isInterceptMenuKey && event.getKeyCode() == KeyEvent.KEYCODE_MENU
                && event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
            dismiss();
            return false;
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialog_bottom_gone:
                if (isDismissOnClickOutSide) {
                    dismiss();
                }
                break;
        }

    }

}
